if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}

$(function(){
  if(AGENT_CLIENT.WEB){
    ready();
  } 
})

function ready() {
    var thisPage = null,
        user = null,
        fromConfirmOrder = getUrlParam('fromConfirmOrder'),
        myscroll = null,
        pullUp = null,
        pullDown = null,
        html = [],
        query = {index: 0};

    var addressPage = function () {
        this.init();
    };

    addressPage.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
          user = getLocalStorage(STORAGE_MANAGER.user);
          if (user) {
              this.addScroll();
              this.loadAddress();
          } else {
              redirect("../account/login.html");
              return false;
          }
        },
        addScroll: function () {
            pullUp = $("#pullUp");
            pullDown = $("#pullDown");
            myscroll = new IScroll('#wrapper', {
                probeType: 2,
                scrollbars: true,
                mouseWheel: false,
                fadeScrollbars: true,
                bounce: true,
                interactiveScrollbars: false,
                shrinkScrollbars: 'scale',
                click: true,
                keyBindings: true,
                momentum: true
            });
            myscroll.on("scrollEnd", function () {
                //console.log("this.y=" + this.y + " this.maxScrollY=" + this.maxScrollY );
                if ((this.y < 0) && (this.y == this.maxScrollY)) {
                    console.log("滚动到底部");
                    pullUp.show();
                    setTimeout(function () {
                        query.index++;
                        thisPage.loadAddress("add");
                    }, 500);
                } else if ((this.y == 0) && (this.y != this.maxScrollY)) {
                  console.log("滚动到顶部");
                  pullDown.show();
                  setTimeout(function() {
                    query.index = 0;
                    thisPage.loadAddress("");
                  }, 500);
                }
            });
        },
        loadAddress: function () {
            $.ajax({
                type: "GET",
                url: CONFIG_MANAGER.RequstUrl + "v1/member/address/" + user.id,
                async: true,
                headers: {"token": user.token},
                beforeSend:function(){ $.showWaiting(); }
            }).done(function (data) {
                thisPage.loadList(data);
            }).fail(function () {
                $.toast("请求出错");
            }).always(function(){
            	$.closeWaiting();
            });

            //加载完成后刷新并隐藏上拉下拉提示
            setTimeout(function () {
                myscroll.refresh();
                pullUp.hide();
                pullDown.hide();
            }, 100);
        },
        loadList: function (data) {
            var list = JSON.parse(data), i = 0, list = list.data, html = [], address = "";
            while (list[i]) {
                html.push('<div class="shdzbg_small" tip=' + list[i].id + '>');
                html.push('<div class="shdzk' + (list[i].default_address ? '' : '1' ) + ' mbtopbottom">');
                html.push('<p class="dwei"><img src="../../images/ditutb' + (list[i].default_address ? '' : '1') + '.png"></p>');
                html.push('<div class="shdzhi">');
                html.push('<p class="sjrmc"><span>' + list[i].mobile + '</span><b class="mrziti' + (list[i].default_address ? '' : '1') + '">' + (list[i].default_address ? '[默认]' : '') + '</b>收件人：' + list[i].consignee + '</p>');
                address = list[i].region_text + ' ' + list[i].address;
                if(address.length>17){
                	address = address.substring(0, 17) + "...";
                }
                html.push('<p ><span class="right-arrow"><a href="#"><i class="iconfont icon-jiantou-right"></i></a></span>' + address + '</p>');
                html.push('</div>');
                html.push('</div>');
                html.push('</div>');
                i++;
            }
            $("#addressList").html(html.join(''));
        },
        bindEvent: function () {
          $("#back").on("click", function () {
          	if(AGENT_CLIENT.APP){
          		back();
          	}else{
          		redirect("account.html");
          	}						
          });

          //点击添加地址
          $("#addAddress").on("click", function () {
          	if(fromConfirmOrder){
          		redirect("addAddress.html?fromConfirmOrder=true");
          	}else{
          		redirect("addAddress.html");
          	}
          });

          //点击编辑
          var t1 = null;
          $("#addressList").on("click", "div.shdzbg_small", function () {
            if (AGENT_CLIENT.APP) {
                if (t1 == null) {
                    t1 = new Date().getTime();
                } else {
                    var t2 = new Date().getTime();
                    if (t2 - t1 < 500) {
                        t1 = t2;
                        return;
                    } else {
                        t1 = t2;
                    }
                }
            }
            var tip = $(this).attr('tip');
            if(fromConfirmOrder){
            	redirect("addAddress.html?id=" + tip + "&fromConfirmOrder=true");
            }else{
            	redirect("addAddress.html?id=" + tip);
            }
            
          });
        }
    };

    thisPage = new addressPage();

}
